package org.hackystat.devcathlon.ui.page.teams.invitations;

import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.team.TeamManager;
import org.hackystat.devcathlon.ui.DevcathlonSession;

/** 
 * Panel that displays all the invitations the user has yet to accept.
 * 
 * @author John Ly
 *
 */
public class InvitationListPanel extends Panel {
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /** 
   * Constructs the Invitation List panel with the wicket id. 
   * @param id The wicket name id.
   */ 
  public InvitationListPanel (String id) {
    super(id);
    
    String email = DevcathlonSession.get().getEmail();
    List<Team> invites = TeamManager.getInstance().getTeamInvites(email);
    
    // Display a message if there are no invites
    String message = "";
    if (invites.isEmpty()) {
      message = "You currently have no invites.";
      DevcathlonSession.get().getTeamsSession().clearSelectedTeam();
    }
    
    add(new Label("InviteMessage", message));
        
    ListView listInvites = new ListView("ListInvites", invites) {

      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(ListItem item) {
        final Team team = (Team) item.getModelObject();
        Link teamLink = new Link ("InvitationLink") {
          private static final long serialVersionUID = 1L;
           
          @Override
          public void onClick() {
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(team.getName());
            setResponsePage(TeamsInvitationPage.class);
          }
        };
        
        teamLink.add(new Label("TeamName", team.getName()));       
        item.add(teamLink);        
      }
    };
    add(listInvites);        
    


  }  

}
